<?php
/**
 * Created by PhpStorm.
 * User: yanggang
 * Date: 2019/5/9
 * Time: 2:31 PM
 */

namespace app\common\org;


use think\Cache;

class Wxjsapi
{
    private $values=array();
   // private $wx_id = 19;
    private $appid = 'wx5894eec1de301b9a';
    private $appsecret = '38e90b1334305c2a147d6e7dc02b33ce';

    public function getConfig($fromurl='',&$out){
        $nocestr = get_encrypt(32);
        $jsapi_ticket = $this->getJsapiTicket();
        if(!$jsapi_ticket)return false;
        $timestamp = time();
        $url = $fromurl;
        $this->setValue('noncestr',$nocestr);
        $this->setValue('jsapi_ticket',$jsapi_ticket);
        $this->setValue('timestamp',$timestamp);
        $this->setValue('url',$url);
        $sign = $this->makeSign();
        $out['nocestr'] = $nocestr;
        $out['jsapi_ticket'] = $jsapi_ticket;
        $out['timestamp'] = $timestamp;
        $out['url'] = $url;
        $out['sign'] = $sign;
        $out['appid'] = trim($this->appid);
        return true;

    }

    public function getJsapiTicket(){
        $jsapi_ticket = Cache::get('jsapi_ticket');
        if(!$jsapi_ticket){
            $access_token = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
            $res = file_get_contents($url);
            $res = json_decode($res,true);
            if($res['errcode'] == 0){
                // 获取成功
                Cache::set('jsapi_ticket',$res['ticket'],7000);
                return $res['ticket'];
            }else{
                return '获取失败';
            }
        }
        return $jsapi_ticket;
    }

    // 获取微信AccessToken
    public function getAccessToken(){
        $access_token = Cache::get('access_token');
        if(!$access_token){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
            $res = file_get_contents($url);
            $res = json_decode($res,true);
            Cache::set('access_token',$res['access_token'],7000);
            return $res['access_token'];
        }
        return $access_token;
    }

    private function setValue($k,$v){
        $this->values[$k] = $v;
    }

    private function getValue(){
        return $this->values;
    }

    private function makeSign(){
        //签名步骤一：按字典序排序参数
        ksort($this->values);
        $string = $this->ToUrlParams();
        $string = iconv("UTF-8", "GB2312//IGNORE", $string);
        return sha1($string);
    }

    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams()
    {
        $buff = "";
        foreach ($this->values as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff [] = $k . "=" . $v;
            }
        }
        $buff = implode('&', $buff);
        $buff = trim($buff, "&");
        return $buff;
    }
}

function http_get($url){
    $oCurl = curl_init();
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if(intval($aStatus["http_code"])==200){
        return $sContent;
    }else{
        return false;
    }
}